package org.lfs.buttons;

import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class ButtonEditor extends DefaultCellEditor {

    private String label;
    private JTable table;

    public ButtonEditor(JCheckBox checkBox, JTable table) {
        super(checkBox);
        this.table = table;
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String textToCopy = table.getValueAt(table.getSelectedRow(), 1).toString();
                Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                Transferable transferable = new StringSelection(textToCopy);
                clipboard.setContents(transferable, null);

                // 创建一个消息对话框并设置为置顶
                JDialog dialog = new JDialog();
                dialog.setAlwaysOnTop(true); // 设置为总是在最上层
                JOptionPane.showMessageDialog(dialog, "已复制到剪贴板");
            }
        });
    }

    protected void fireEditingStopped() {
        super.fireEditingStopped();
    }

    private JButton button = new JButton();

    public Component getTableCellEditorComponent(JTable table, Object value,
                                                 boolean isSelected, int row, int column) {
        label = (value == null) ? "" : value.toString();
        button.setText(label);
        return button;
    }

    public Object getCellEditorValue() {
        return new String(label);
    }
}